import { defineStore } from 'pinia'
import { userInfo } from '@/apis/personal';

export const useUserStore = defineStore('users', {
  // 类似于data
  state: () => ({
    token: '',
    userInfo: {},
  }),
  // 类似于computed计算属性
  getters: {
    isLogin(state){
      return !!state.token || !!localStorage.getItem('pm_token');
    }
  },
  // 类似于vue2中的methods
  actions: {
    setToken(token){
      localStorage.setItem('pm_token', token);
      this.token = token;
    },
    clearToken(){
      this.token = '';
      localStorage.removeItem('pm_token');
    },
    setUserInfo( info) {
      this.userInfo = info || {};
    },
    clearUserInfo(){
      this.userInfo = {};
    },
    refreshInfo(){
      userInfo().then(res => {
        this.setUserInfo(res.data);
      });
    },
  },
})

